package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.pojo.Setmeal;
import com.itheima.reggie.result.R;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    /*
    * 新增套餐
    * */
    @PostMapping
    public R<String> add(@RequestBody SetmealDto setmealDto) {
        log.info(setmealDto.toString());

        setmealService.addSetmealDish(setmealDto);
        return R.success("新增成功！");
    }

    /*
     * 分页查询
     * */
    @GetMapping("/page")
    public R<Page> selectSetmealDish(int page, int pageSize, String name) {
        return setmealService.selectSetmealDish(page, pageSize, name);
    }

    /*
     * 根据id查询套餐信息
     * */
    @GetMapping("/{id}")
    public R<SetmealDto> selectById(@PathVariable Long id) {
        SetmealDto setmealDto = setmealService.selectById(id);

        return R.success(setmealDto);
    }

    /*
     * 修改套餐
     * */
    @PutMapping
    public R<String> updateSetmeal(@RequestBody SetmealDto setmealDto) {

        setmealService.updateSetmeal(setmealDto);

        return R.success("修改成功！");
    }

    /*
     * 删除套餐
     * */
    @DeleteMapping
    public R<String> deleteByIds(@RequestParam List<Long> ids) {
        setmealService.deleteSetmeal(ids);

        return R.success("删除成功！");
    }

    /*
     * 起售/禁售
     * */
    @PostMapping("/status/{status}")
    public R<String> status(@PathVariable Integer status, Long[] ids) {
        setmealService.status(status,ids);

        return R.success("售卖状态切换成功！");
    }

    /*
     * 查询套餐数据
     * */
    @GetMapping("/list")
    public R<List<Setmeal>> list(Setmeal setmeal) {
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.eq(setmeal.getCategoryId() != null, Setmeal::getCategoryId, setmeal.getCategoryId())
                .eq(setmeal.getStatus() != null, Setmeal::getStatus, setmeal.getStatus())
                .orderByDesc(Setmeal::getUpdateTime);

        List<Setmeal> setmealList = setmealService.list(lqw);

        return R.success(setmealList);
    }
}
